package gugu.framework.core;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import org.springframework.core.convert.converter.Converter;

/**
 * This convert will convert the Calendar (to local user with the local time zone) to a string label (based on UTC timezone)
 * and the origin time zone will concat to the end of the converted string label,for example
 * the output string label will looks like
 * 04/07/2012 03:49:45 UTC$America/Los_Angeles
 * the origin calendar time zone is America/Los_Angeles
 * 
 * @author jicui
 *
 */
public class CalendarToStringLabel implements Converter<Calendar, String> {
	private static final String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss z";// 1-24hours
	private static TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
	private static final SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

	@Override
	public String convert(Calendar arg0) {
		Calendar cloned=(Calendar)arg0.clone();
		String orgineTimeZone=cloned.getTimeZone().getID();
		formatter.setCalendar(cloned);
		formatter.setTimeZone(utcTimeZone);
		StringBuffer sbf=new StringBuffer();
		sbf.append(formatter.format(cloned.getTime())).append("#").append(orgineTimeZone);
		return sbf.toString();
	}

	public static void main(String[] args) {
		String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss z";
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT,
				Locale.US);
		GregorianCalendar cal = new GregorianCalendar(
				TimeZone.getTimeZone("America/Los_Angeles"), Locale.UK);
		System.out.println(cal.getTimeZone().getID());
		formatter.setCalendar(cal);
		formatter.setTimeZone(utcTimeZone);
		String output = formatter.format(cal.getTime());
		System.out.println(output+ " ("+cal.getTimeZone().getID()+")");
	}

}
